/*
	Author      : Sidek Sirun
	Course      : UCI Java Programming I (X460.10)
	Assignment 6: Person Text File

	Create a text file that contains 5 Person objects, each represented 
	on a separate line with each attribute separated by a comma. Create 
	a Java program that reads from the file, creates one Person object 
	per line, and stores the object in a collection. Write the objects 
	out sorted by last name to the console.
*/


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class FileIOTest {

	/**
	* @lastNameCompare() method
	*/
	public static class lastNameCompare implements Comparator<Person> {
		public int compare(Person p1, Person p2) {
			return p1.getLastName().compareTo(p2.getLastName());
		}
	}

	/**
	* @main() method
	*/
	public static void main(String[] args) {
	
		File     personFileHandler;
		Scanner  fileScanner;
		String   rdata;
		String[] dataList = new String[] {};

		// Person collection
		ArrayList<Person>  personObjectList = new ArrayList<Person>();
		// Person attributes
		String  firstName = "";
		String  lastName  = "";
		int     age       = 0;
		String  gender    = "";
		String  ssn       = "";

		try {
			personFileHandler  = new File("personData.txt");
			fileScanner        = new Scanner(personFileHandler);
			//fileScanner.useDelimiter(",");

			while (fileScanner.hasNextLine()) {
				rdata    = fileScanner.nextLine();
				dataList = rdata.split(",");
				//debug//System.out.println(rdata);

				// Populate person infomation from file
				for (int i=0; i < dataList.length; i++) {
					firstName = dataList[0];
					lastName  = dataList[1];
					age       = Integer.parseInt(dataList[2]);
					gender    = dataList[3];
					ssn       = dataList[4];
					//System.out.println(dataList[i]);
				}

				//debug//System.out.println("dataList array length: " + dataList.length);
				// Person object
				Person p = new Person(firstName, lastName, age, gender, ssn);
				// add person object to personObjectList
				personObjectList.add(p);

			}

			fileScanner.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		// Print the personObjectList content to STDOUT
		System.out.println("");
		System.out.println("Person data before sorting by last name");
		System.out.println("---------------------------------------");
		for (int j=0; j < personObjectList.size(); j++) {
			//System.out.println(personObjectList.get(j).toString());
			String pFName  = personObjectList.get(j).getFirstName();
			String pLName  = personObjectList.get(j).getLastName();
			int    pAge    = personObjectList.get(j).getAge();
			String pGender = personObjectList.get(j).getGender();
			String pSSN    = personObjectList.get(j).getSSN();
			System.out.println("Last Name: " + pLName + ", First Name: " + pFName + ", Age: " + pAge + ", Gender: " + pGender + ", SSN: " + pSSN);
		}

		// Sort person object list by last name
		Collections.sort(personObjectList, new lastNameCompare());
		// Another sorting method without creating class
		//Collections.sort(personObjectList, new Comparator<Person>() {
		//	public int compare(Person p1, Person p2) {
		//		return p1.getLastName().compareTo(p2.getLastName());
		//	}
		//});

		System.out.println("");
		System.out.println("Person data after sorting by last name");
		System.out.println("--------------------------------------");
		for (int j=0; j < personObjectList.size(); j++) {
			//System.out.println(personObjectList.get(j).toString());
			String pFName  = personObjectList.get(j).getFirstName();
			String pLName  = personObjectList.get(j).getLastName();
			int    pAge    = personObjectList.get(j).getAge();
			String pGender = personObjectList.get(j).getGender();
			String pSSN    = personObjectList.get(j).getSSN();
			System.out.println("Last Name: " + pLName + ", First Name: " + pFName + ", Age: " + pAge + ", Gender: " + pGender + ", SSN: " + pSSN);
		}
		System.out.println("");

	}
}
